//
//  NotesTableViewController.m
//  Cachalot
//
//  Created by Adam Cue on 3/31/11.
//  Copyright Adam Cue 2011.
//
//  This program is licensed and distributed under the terms of the GNU General Public License.  
//	The text for the license can be found in the license.txt file in this project.
//

#import "NotesTableViewController.h"
#import "Note.h"

@interface NotesTableViewController (Private)
- (void)emailNotes;
@end

@implementation NotesTableViewController
@synthesize listOfNotes;
@synthesize readingID;
@synthesize dimensionOfPage;
@synthesize delegate;
@synthesize lessonID;

- (void)viewDidLoad {
   [super viewDidLoad];
	self.tableView.backgroundColor = [UIColor darkGrayColor];
	self.tableView.separatorColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1];
}

- (void)viewWillAppear:(BOOL)animated {
   [super viewWillAppear:animated];
	NSFetchRequest *noteRequest = [[NSFetchRequest alloc] init];
	NSEntityDescription *noteEntity = [NSEntityDescription entityForName:@"Note" inManagedObjectContext:[[MarineFetcher sharedInstance] managedObjectContext]];
	[noteRequest setEntity:noteEntity];
	if(readingID != 0){
		[noteRequest setPredicate:[NSPredicate predicateWithFormat:@"readingID == %@", [NSNumber numberWithInt:readingID]]];
	}
	else if(lessonID != 0){
		[noteRequest setPredicate:[NSPredicate predicateWithFormat:@"lessonID == %@", [NSNumber numberWithInt:lessonID]]];
	}
	NSError *noteError;
	NSArray *noteFetchResults = [[[MarineFetcher sharedInstance] managedObjectContext] executeFetchRequest:noteRequest error:&noteError];
	if (noteFetchResults == nil) {
		NSLog(@"%@", noteError);
	}
	if(readingID != 0){
		self.listOfNotes = [noteFetchResults sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"y" ascending:YES]]];
	}
	else if(lessonID != 0){
		self.listOfNotes = [noteFetchResults sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"x" ascending:YES]]];
	}
	[self.tableView reloadData];
}

- (void)dealloc {
   self.listOfNotes = nil;
   self.delegate = nil;
   
   [super dealloc];
}

#pragma mark UserActions
- (void)emailNotes{
	NSMutableString *allText = [NSMutableString stringWithCapacity:4];
	[allText appendFormat:@"<ul>"];
	for(Note *note in listOfNotes){
		[allText appendFormat:@"<li>%@</li>", note.text];
	}
	[allText appendFormat:@"</ul>"];
	[delegate allNotesEmailed:allText];
}

#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [listOfNotes count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   static NSString *CellIdentifier = @"NotesTableViewCell";
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   if (cell == nil) {
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
      cell.textLabel.backgroundColor = [UIColor clearColor];
      cell.textLabel.textColor = [UIColor whiteColor];
      cell.detailTextLabel.textColor = [UIColor whiteColor];
      cell.detailTextLabel.backgroundColor = [UIColor clearColor];
   }
   Note *note = [listOfNotes objectAtIndex:indexPath.row];
	cell.textLabel.text = [note text];	
   cell.imageView.image = [UIImage imageNamed:@"sticky"];
	if(readingID != 0){
		cell.detailTextLabel.text = [NSString stringWithFormat:@"p.%d", ([note.y intValue] / dimensionOfPage) + 1];
	}
	else if(lessonID != 0){
		cell.detailTextLabel.text = [NSString stringWithFormat:@"p.%d", ([note.x intValue] / dimensionOfPage) + 1];
	}
   return cell;
}

#pragma mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	Note *note = [listOfNotes objectAtIndex:indexPath.row];
    [delegate noteClicked:note];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
	cell.backgroundColor = [UIColor colorWithRed:0.333 green:0.333 blue:0.333 alpha:1];
	for(UIView *subview in cell.subviews){
		subview.backgroundColor = [UIColor clearColor];
	}
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
	return 44;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
	UIToolbar *emailBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
	emailBar.tintColor = [UIColor orangeColor];
	UIBarButtonItem *flexibleSpace = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
	UIBarButtonItem *emailBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Email Notes" style:UIBarButtonItemStyleBordered target:self action:@selector(emailNotes)];
	[emailBar setItems:[NSArray arrayWithObjects:flexibleSpace, emailBarButtonItem, nil]];
	[emailBarButtonItem release];
	[emailBar autorelease];
	return emailBar;
}

@end

